<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Http\Resources\GoodsHomeSearchCollection;

class IndexController extends Controller
{
    // App 首页
    public function app_base(){

        // 如果翻页则不再请求其他数据
        if(!isset(request()->page) || !(request()->page>1)){
            $data['goods_class'] = $this->getService('GoodsClass',true)->select('id','thumb','name')->where('pid',0)->take(5)->orderBy('is_sort','asc')->get(); // 顶级栏目
            // 去掉&符号和空格 然后取前四位
            foreach($data['goods_class'] as &$goods_class_item){
                $goods_class_item['name'] = str_replace(['&',' '],'',$goods_class_item['name']);
                $goods_class_item['name'] = mb_substr($goods_class_item['name'],0,4);
            }

            $data['banner'] = []; // $adv_service->getAdvList('APP_幻灯片')['data'];// app 幻灯片
            $data['nav_bottom_adv'] = []; // $adv_service->getAdvList('APP_栏目底部广告'); // 栏目底部广告
        }
        $data['hot_goods'] = new GoodsHomeSearchCollection($this->getService('Goods',true)->whereHas('store',function($q){
            return $q->where(['store_status' => 1, 'store_verify' => 4]);
        })->with(['goods_skus'=>function($q){
            return $q->orderBy('goods_price','asc');
        }])->where(['goods_status' => 1, 'goods_verify' => 1])->orderBy('id','desc')->paginate(request()->per_page??30));// 热门产品

        return $this->success($data);
    }
}
